package com.tian.arch.base

import android.app.Activity
import android.app.Application
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import java.util.Stack

/**
 *  activity管理类
 */
class AcManager : Application.ActivityLifecycleCallbacks {

    private val acs = Stack<AppCompatActivity>()

    override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
        acs.add(activity as AppCompatActivity)
    }

    override fun onActivityStarted(activity: Activity) {
    }

    override fun onActivityResumed(activity: Activity) {
    }

    override fun onActivityPaused(activity: Activity) {
    }

    override fun onActivityStopped(activity: Activity) {
    }

    override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {
    }

    override fun onActivityDestroyed(activity: Activity) {
        acs.remove(activity as AppCompatActivity)
    }

    /**
     * 获取栈顶activity
     */
    fun topActivity(): AppCompatActivity {
        return acs.peek()
    }

    companion object {
        val manager by lazy { AcManager() }
    }

}
